package com.zsl.controller.Admin;

import com.alibaba.fastjson.JSON;

import com.zsl.model.Book;
import com.zsl.service.BookService;
import com.zsl.service.FileService;
import com.zsl.service.impl.BookServiceImpl;
import com.zsl.service.impl.FileServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@WebServlet(name = "OpBook", urlPatterns = "/op/book")
public class OpBook extends HttpServlet {
    FileService fileService = new FileServiceImpl();
    BookService bookService = new BookServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Map<String, Object> res = new HashMap<>();
        //获取数据
        String service = request.getParameter("service").equals("") ? null : request.getParameter("service");
        String b_name = request.getParameter("b_name").equals("") ? null : request.getParameter("b_name");
        String author = request.getParameter("service").equals("") ? null : request.getParameter("author");
        String price = request.getParameter("service").equals("") ? null : request.getParameter("price");
        String c_id = request.getParameter("c_id").equals("") ? null : request.getParameter("c_id");
        String b_description = request.getParameter("service").equals("") ? null : request.getParameter("b_description");
        String image = null;
        if (service == null || b_name == null || author == null || price == null || c_id == null || b_description == null) {
            res.put("code", 0);
            res.put("line", 0);
            res.put("msg", "输入错误");
            PrintWriter out = response.getWriter();
            out.write(JSON.toJSONString(res));
            return;
        }
        //插入书籍
        if (service.equals("insert")) {
            // 读取文件，生成文件id名
            try {
                image = fileService.uploadJPGEFile(request);
                if (image == null) {
                    res.put("code", 0);
                    res.put("line", 0);
                    res.put("msg", "上传文件失败");
                    PrintWriter out = response.getWriter();
                    out.write(JSON.toJSONString(res));
                    return;
                }
                //生成一个Book类
                Book book = new Book(UUID.randomUUID().toString(), b_name, author, Float.parseFloat(price), image, b_description, c_id);
                System.out.println(book.toString());
                //TODO 上传书本
                res = bookService.insertBook(book);
            } catch (Exception e) {
                e.printStackTrace();
                res.put("code", 0);
                res.put("line", 0);
                res.put("msg", "输入错误");
            } finally {
                PrintWriter out = response.getWriter();
                out.write(JSON.toJSONString(res));
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

